package fy.mdm.dao;

import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.soa.eis.adapter.framework.message.IMsgObject;
import com.soa.eis.adapter.framework.message.impl.GroupRecord;
import com.soa.eis.adapter.framework.message.impl.MsgObject;
import com.soa.eis.adapter.framework.requester.IServiceRequester;
import com.soa.eis.adapter.framework.requester.impl.BaseServiceRequester;

import fy.config.FyConfig;
import fy.mdm.bean.SampleReviewBean;
import fy.util.DBUtil;

public class SampleReviewDao {
	private final static String sql = " select code from org_unit where type='Account'and id=? ";
	private final static String FLOW_ID_GET = "select AFFAIR_ID from CTP_COMMENT_ALL where MODULE_ID=?";
	private SampleReviewBean sampleReviewBean;
	private String purchaseStatus;// 采购状态转化
	/*
	 * <?xml version="1.0" encoding="UTF-8"?>
	 * 
	 * <Service>   <Route>     <ServiceResponse/>
	 *     <SourceSysID>06001</SourceSysID>
	 *     <SerialNO>2017062206001000007</SerialNO>
	 *     <ServiceID>06001000000003</ServiceID>
	 *     <ServiceTime>20170622081723</ServiceTime>   </Route>   <Data>
	 *     <Control/>     <Request>       <BOID>MDM_MAT_PURCHASE_STATUS_B</BOID>
	 *       <ROW p_type="G" loop_num="1">
	 *         <MATERIEL_CODE>BGC0042G</MATERIEL_CODE>
	 *         <MATERIEL_NAME>防弹材料</MATERIEL_NAME>
	 *         <PURCHASE_STATUS>多选</PURCHASE_STATUS>
	 *         <SUBSIDIARY>FYWD</SUBSIDIARY>
	 *         <EVALUATE_MAN>cc</EVALUATE_MAN>
	 *         <EVALUATE_TIME>1987-07-11</EVALUATE_TIME>
	 *         <ENABLE_STATUS>Ebable</ENABLE_STATUS>       </ROW>     </Request>
	 *     <Response/>   </Data> </Service>
	 */

	public boolean StateReturn() throws Exception {
		try {
			/*
			 * System.out.println(sampleReviewBean.getMaterialCode() +
			 * sampleReviewBean.getApplier() + "!!!!" +
			 * sampleReviewBean.getPurchaseStatus() +
			 * sampleReviewBean.getSubsidiary() + sampleReviewBean.getTs());
			 */
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String[] strs = sampleReviewBean.getSubsidiary().split(",");
			for (int i = 0; i < strs.length; i++) {
				if (sampleReviewBean.getPurchaseStatus() == 0) {
					purchaseStatus = "首件";
				} else if (sampleReviewBean.getPurchaseStatus() == 1) {
					purchaseStatus = "小批量";
				} else {
					purchaseStatus = "批量";
				}

				@SuppressWarnings("deprecation")
			/*	String todo_view = "http://10.112.0.171/seeyon/login/oatodo?system=OA&uid=MDM" + "&dest="
						+ URLEncoder
								.encode("/seeyon/collaboration/collaboration.do?method=summary&openFrom=listDone&affairId="
										+ getAffairID(this.sampleReviewBean.getSummaryId()));*/
				//正式
				String todo_view = "http://10.111.0.54/seeyon/login/oatodo?system=OA&uid=MDM" + "&dest="
				+ URLEncoder
						.encode("/seeyon/collaboration/collaboration.do?method=summary&openFrom=listDone&affairId="
								+ getAffairID(this.sampleReviewBean.getSummaryId()));
				/*System.out.println(
						this.sampleReviewBean.getBillname() + this.sampleReviewBean.getSummaryId() + todo_view);*/
				// getAffairID(this.sampleReviewBean.getSummaryId());
				IServiceRequester Requester = BaseServiceRequester.getInstance();
				List<GroupRecord> grs1 = new ArrayList<GroupRecord>();
				// 定义请求报文对象
				IMsgObject reqMo = new MsgObject(IMsgObject.MOType.initSR);
				String SourceSysID = "06004"; // 请求方(发布方)
				String ServiceID = "06001000000003";// 响应方(发布方)
				// 获取系统时间
				Date date = new Date();
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
				String format = simpleDateFormat.format(date);
				// 对请求报文对象进行设置
				reqMo.setSourceSysID(SourceSysID); // 设置报文 请求方系统号
				reqMo.setSerialNO(format.substring(0, 8) + SourceSysID
						+ String.format("%06d", ++MaterialRequestNoTimerTask.requestNo));// 设置报文流水号,第三段表示今天发送的第几的一次报文
				reqMo.setServiceID(ServiceID); // 设置报文 提供方服务号
				reqMo.setServiceDateTime(format); // 设置报文 日期时间
				// reqMo.setReqValue("BOID", "MDM_MAT_PURCHASE_STATUS_B");
				// MDM_MATERIEL_B_F
				//System.out.println("====" + sampleReviewBean.getFirst_category());
				reqMo.setReqValue("BOID", sampleReviewBean.getFirst_category());
				// reqMo.setReqValue("MATERIEL_CODE",codes);
				GroupRecord gr1 = new GroupRecord();
				gr1.setName("ROW");
				gr1.setFieldValue("MATERIEL_CODE", sampleReviewBean.getMaterialCode());
				gr1.setFieldValue("MATERIEL_NAME", sampleReviewBean.getMaterialName());
				gr1.setFieldValue("PURCHASE_STATUS", purchaseStatus);
				gr1.setFieldValue("SUBSIDIARY", getUnitCode(strs[i]));
				gr1.setFieldValue("EVALUATE_URL", todo_view);
				gr1.setFieldValue("EVALUATE_TIME", sdf.format(new Date()));
				gr1.setFieldValue("ENABLE_STATUS", "Enable");
				grs1.add(gr1);
				reqMo.setReqGroupRecord(grs1);
				//System.out.println(reqMo);
				IMsgObject respMo = null;
				respMo = Requester.execute(reqMo);
				//System.out.println("~~~~~~~~~~~~~~~" + respMo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	// 获取组织code
	public String getUnitCode(String code) {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;

		try {
			conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());
			pst = conn.prepareStatement(sql);
			pst.setString(1, code);
			rs = pst.executeQuery();
			if (rs.next()) {
				code = rs.getString(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, pst);
		}
		return code;
	}

	public SampleReviewBean getSampleReviewBean() {
		return sampleReviewBean;
	}

	public void setSampleReviewBean(SampleReviewBean sampleReviewBean) {
		this.sampleReviewBean = sampleReviewBean;
	}

	// 获取flowID
	public String getAffairID(long flowID) {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		String affairID = null;
		try {
			conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());
			pst = conn.prepareStatement(FLOW_ID_GET);
			pst.setLong(1, flowID);
			rs = pst.executeQuery();
			if (rs.next()) {
				affairID = rs.getString(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, pst);
		}
		return affairID;
	}

}
